from flask import Flask, request, jsonify
from resemblyzer import VoiceEncoder
import numpy as np
import soundfile as sf

app = Flask(__name__)

encoder = VoiceEncoder()

# 加载预设声纹
reference_wav, _ = sf.read("reference_voice.wav")
reference_embedding = encoder.embed_utterance(reference_wav)

@app.route('/verify', methods=['POST'])
def verify():
    file = request.files['audio']
    input_wav, _ = sf.read(file)
    input_embedding = encoder.embed_utterance(input_wav)
    similarity = np.dot(reference_embedding, input_embedding)
    is_match = similarity > 0.85  # 相似度阈值
    return jsonify({"match": is_match, "similarity": similarity})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)